﻿namespace Resharper.Abstractions.CodeDom.Statements
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.Tree;
  using Resharper.Abstractions.Actions;

  /// <summary>Defines the <see cref="StatementExtensions"/> class.</summary>
  public static class StatementExtensions
  {
    #region Public Methods

    /// <summary>Creates the statement.</summary>
    /// <param name="factory">The factory.</param>
    /// <param name="code">The code.</param>
    /// <returns>Returns the statement.</returns>
    [NotNull]
    public static Statement CreateStatement([NotNull] this Factory factory, [NotNull] string code)
    {
      if (factory == null)
      {
        throw new ArgumentNullException("factory");
      }

      if (code == null)
      {
        throw new ArgumentNullException("code");
      }

      return StatementFactory.GetStatement(factory.InternalFactory.CreateStatement(code));
    }

    /// <summary>Gets the selected statement.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the selected statement.</returns>
    [CanBeNull]
    public static Statement GetSelectedStatement([NotNull] this ProviderContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var statement = context.InternalGetSelectedElement<IStatement>();
      
      return statement != null ? StatementFactory.GetStatement(statement) : null;
    }

    #endregion
  }
}